package cn.therouter.idea.plugin

import com.intellij.psi.PsiElement
import org.intellij.markdown.flavours.gfm.table.GitHubTableMarkerProvider.Companion.contains

fun getRouteAnnotationCode(element: PsiElement): TargetContent? {
    if (isRouteAnnotation(element)) {
        val content = element.text.replace(" ", "")
            .replace("\n", "")
            .replace("@Route(", "")
            .replace(")", "")

        val allParams = content.split(",")

        var path = ""
        allParams.forEach {
            if (it.startsWith("path=")) {
                path = it.substring("path=".length)
            }
        }
        if (path.isNotBlank()) {
            if (path.contains('.') && !path.contains('"')) {
                val index = path.lastIndexOf('.')
                if (index > 0) {
                    path = path.substring(index, path.length)
                    debug("getRouteAnnotationCode", path)
                }
            }
            debug("getRouteAnnotationCode2", path)
            return TargetContent(TYPE_ANNOTATION, path)
        }
    }
    return null
}

fun getActionInterceptorCode(psiElement: PsiElement): TargetContent? {
    if (psiElement.javaClass.name == "org.jetbrains.kotlin.psi.KtDotQualifiedExpression"
        || psiElement.javaClass.name == "com.intellij.psi.impl.source.tree.java.PsiMethodCallExpressionImpl"
        || psiElement.javaClass.name == "com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl"
    ) {
        if (isTheRouterAddActionInterceptor(psiElement)) {
            var path = matchActionInterceptor(psiElement.text)
            if (path.isNotBlank()) {
                if (path.contains('.') && !path.contains('"')) {
                    val index = path.lastIndexOf('.')
                    if (index > 0) {
                        path = path.substring(index, path.length)
                        debug("getActionInterceptorCode", path)
                    }
                }
                debug("getActionInterceptorCode2", path)
                return TargetContent(TYPE_ACTION, path)
            }
        }
    }
    return null
}

fun getNavigationCode(psiElement: PsiElement): TargetContent? {
    if (isTheRouterBuild(psiElement)) {
        var path = matchBuild(psiElement.text.replace(" ", ""))
        if (path.isNotBlank()) {
            if (path.contains('.') && !path.contains('"')) {
                val index = path.lastIndexOf('.')
                if (index > 0) {
                    path = path.substring(index, path.length)
                    debug("getNavigationCode", path)
                }
            }
            debug("getNavigationCode2", path)
            return TargetContent(TYPE_NAVIGATION, path)
        }
    }
    return null
}

fun isTheRouterBuild(psiElement: PsiElement, path: String = ""): Boolean {
    val content = psiElement.text.replace(" ", "").replace("\n", "")
    val prefix = if (path.isEmpty()) {
        "TheRouter.build("
    } else {
        "TheRouter.build($path)"
    }

    return content.startsWith(prefix) && content.endsWith(")") &&
            (content.contains("navigation") ||
                    content.contains("action") ||
                    content.contains("createFragment") ||
                    content.contains("createIntent") ||
                    content.contains("getUrlWithParams"))
}

fun isTheRouterAddActionInterceptor(psiElement: PsiElement, path: String = ""): Boolean {
    val content = psiElement.text.replace(" ", "").replace("\n", "")

    val prefix = if (path.isEmpty()) {
        "TheRouter.addActionInterceptor("
    } else {
        "TheRouter.addActionInterceptor($path,"
    }
    return content.startsWith(prefix)
}

fun isRouteAnnotation(psiElement: PsiElement, path: String = ""): Boolean {
    val content = psiElement.text.replace(" ", "").replace("\n", "")
    val containPath = if (path.isEmpty()) {
        content.contains("path=")
    } else {
        content.contains("path=$path,") || content.contains("path=$path)")
    }

    if (containPath && content.startsWith("@Route(") && content.endsWith(")")) {
        val str = content.replaceFirst("@Route(", "")
        return !str.contains("@Route")
    }
    return false
}